﻿namespace AnalyStock.GrapImage;
using static CandlePointSet;
/// <summary>
///     定义蜡烛线属性值，包括颜色，类型枚举，倍量值
/// </summary>
public static class CandlePointSet
{
    public enum PeriodType
    {
        Daily,
        Weekly,
        Monthly
    }
    private readonly record struct CandleOrVolColor(
        SolidColorBrush RaisePrice,
        SolidColorBrush DownPrice,
        SolidColorBrush RaiseVolume,
        SolidColorBrush DownVolume,
        SolidColorBrush SkyrocketingPrice);
    internal static float DoubleVolMultiples { get; set; } = 1.85f; //交易量大于上日交易量的倍值

    private static readonly CandleOrVolColor[] CandleOrVolColors =
    {
        new(Brushes.Red, Brushes.DarkTurquoise, Brushes.DarkOrange, Brushes.MediumTurquoise,
            Brushes.Orange), //ColorTypeNo=0                 
        new("#E00000".ConvertoBrush(), "#20A000".ConvertoBrush(), Brushes.DarkOrange, "#00A000".ConvertoBrush(),
            Brushes.Orange),
        new(Brushes.Crimson, Brushes.DarkCyan, Brushes.DarkSalmon, Brushes.DarkCyan, Brushes.DarkOrange),
        new(Brushes.DarkOrange, Brushes.SteelBlue, Brushes.DarkOrange, Brushes.SteelBlue, Brushes.Crimson)
    };

    private static int ColorType { get; } = 1;

    internal static SolidColorBrush SetCandleBarColor(bool isPriceRaise)
    {
        return isPriceRaise ? CandleOrVolColors[ColorType].RaisePrice : CandleOrVolColors[ColorType].DownPrice;
    }

    internal static SolidColorBrush SetVolumeBarColor(bool isPriceRaise)
    {
        return isPriceRaise ? CandleOrVolColors[ColorType].RaiseVolume : CandleOrVolColors[ColorType].DownVolume;
    }

    internal static SolidColorBrush SetSkyrocketingPriceColor()
    {
        return CandleOrVolColors[ColorType].SkyrocketingPrice;
    }

    internal static string GetWeekIndexInYear(string trade_date)
    {
        var tradeDate = trade_date.ConvertToDate();
        var startDate = $"{trade_date[..4]}0101".ConvertToDate();
        var dayOfWeekFirstday = (int)startDate.DayOfWeek;
        var mondayOfFirstweek = startDate.AddDays(1 - dayOfWeekFirstday); //确定当日周一日期
        var indexOfWeekOnYear = (tradeDate - mondayOfFirstweek).Days / 7;
        var indexWeek = indexOfWeekOnYear < 10 ? $"0{indexOfWeekOnYear}" : $"{indexOfWeekOnYear}";
        return $"{trade_date[..4]}{indexWeek}";
    }

    
}

/// <summary>
///     蜡烛线
/// </summary>

public class CandlePoint
{
    public int LocationIndex { get; set; }
    public string Month => Date[..6];
    public string StockCode { get; set; }
    public string Name { get; set; }
    public string Date { get; set; }
    public string Week => GetWeekIndexInYear(Date); //Year+Noofweek
    public float High { get; set; }
    public float Low { get; set; }
    public float Open { get; set; }
    public float Close { get; set; }
    public float Vol { get; set; } //交易量：手（百股)  
    public float LastVol { get; set; } //上一交易日交易量
    public float FrontlastVol { get; set; } //上上一交易日交易量
    public float Amount { get; set; } //交易额：千元        
    public float Change { get; set; } //增长额
    public float Pctchg { get; set; } //增幅
    public float TurnOverRate { get; set; } //换手率
    public float AdjustPowerFactor { get; set; } //复权因子
    public PeriodType CandleType { get; set; } = 0; //0:日线，1：周线，2：月线
    public bool IsCloseUpOpen => Close >= Open;
    public bool IsDoubleVol { get; set; }
    public bool IsSkyrocketing => CandleType switch
    {
        PeriodType.Daily => Pctchg >= 9.90f, //日线涨幅超过9.95%，设置涨停标识
        PeriodType.Weekly => Pctchg >= 30.0f, //周线涨幅超过30.0%，设置涨停标识
        PeriodType.Monthly => Pctchg >= 50.0f, //月线涨幅超过50.0%，设置涨停标识
        _ => false
    };

    public SolidColorBrush VolColor => SetVolumeBarColor(IsCloseUpOpen);
    public SolidColorBrush CandleColor => SetCandleBarColor(IsCloseUpOpen);

    public object[] TextPriceTable()
    {
        return [Date, Open, Close, High, Low, Vol, Pctchg, TurnOverRate];
    }
}

/// <summary>
///     均线类
/// </summary>
public class MAPoint
{
    public int Clcye;
    public int LocationIndex; //坐标轴上的索引位置，不是具体索引
    public int MAIndex; //0开始的数据组索引
    public float MAValue;
    public float STDValue;
    public string Type; //MA,EMA,Boll
}

public class KDJPoint
{
    public float DValue;
    public float JValue;
    public int KDJIndex;
    public float KValue;
    public int LocationIndex;
    public float RSValue;
}

public class MACDPoint
{
    public float DEAValue;
    public float DIFValue;
    public float EMALongValue;
    public float EMAShortValue;
    public int LocationIndex;
    public int MACDIndex;
    public float MACDValue;
}

public class TurnOverRate(int turnOverRateIndex, int locationIndex, float turnOverRateVal)
{
    public int LocationIndex = locationIndex;
    public int TurnOverRateIndex = turnOverRateIndex;
    public float TurnOverRateVal = turnOverRateVal;
}

//计算量比quantity relative ratio
public class QRRatio(int qRRatioIndex, int locationIndex, float qRRatioVal)
{
    public int LocationIndex = locationIndex;
    public int QRRatioIndex = qRRatioIndex;
    public float QRRatioVal = qRRatioVal;
}

public class PEPoint
{
    public int LocationIndex { get; set; }
    public float EPS { get; set; }
    public float PE { get; set; }
}

public class PBPoint
{
    public int LocationIndex { get; set; }
    public float NetAssetPS { get; set; }
    public float PB { get; set; }
}